import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load your Excel file
file_path = r'C:\Users\SBD Lab\Desktop\BE_2024_monthly.xlsx'
df = pd.read_excel(file_path)

# Clean and prepare the data
df.columns = ['Datetime', 'Carbon_intensity']
df['Datetime'] = pd.to_datetime(df['Datetime'], dayfirst=True, errors='coerce')
df.dropna(subset=['Datetime', 'Carbon_intensity'], inplace=True)

# Extract month number
df['Month'] = df['Datetime'].dt.month

# Get average carbon intensity per month
monthly_avg = df.groupby('Month')['Carbon_intensity'].mean().reindex(range(1, 13))

# Prepare heatmap data
heatmap_data = [monthly_avg.values]

# Create heatmap
plt.figure(figsize=(14, 2))
ax = sns.heatmap(
    heatmap_data,
    cmap='RdYlGn_r',
    cbar=True,
    vmin=0,
    vmax=300,
    cbar_kws={
        'orientation': 'horizontal',
        'label': 'Carbon Intensity [gCO₂/kWh]',
        'pad': 0.15  # increased padding slightly
    }
)

# Set month labels
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
plt.xticks(ticks=[i + 0.5 for i in range(12)], labels=month_names, fontsize=12)
plt.yticks([])  # Hide y-axis

# Title and label below x-axis
plt.title('Monthly Carbon Intensity (Belgium 2024)', fontsize=16, fontweight='bold')

# Adjust x-axis label padding for better visibility
plt.xlabel('Month (2024)', fontsize=13, labelpad=12)  # slightly more padding

# Adjust the bottom spacing so everything fits well
plt.subplots_adjust(bottom=0.28)  # increased bottom space

plt.tight_layout()
plt.show()
